package com.morningstar.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 统一响应消息报文
 * @author yls
 */
@Data
public class Result<T> {
	/**
	 * 响应码
	 */
	private int code;
	/**
	 * 响应消息
	 */
	private String msg;
	/**
	 * 时间戳
	 */
	private long timestamp;
	/**
	 * 异常对象
	 */
	private String exception;
	/**
	 * 响应数据
	 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private T data;

	/***************************构造方法******************************/
	private Result() {
		this.timestamp = System.currentTimeMillis();
	}

	private Result(IResultCode resultCode) {
		this(resultCode, "");
	}

	private Result(IResultCode resultCode, String exception) {
		this(resultCode, exception, null);
	}

	private Result(IResultCode resultCode, T data) {
		this(resultCode, "", data);
	}

	private Result(IResultCode resultCode, String exception, T data) {
		this(resultCode.getStatus(), resultCode.getMessage(), exception, data);
	}

	private Result(int code, String msg, String exception, T data) {
		this.code = code;
		this.msg = msg;
		this.timestamp = System.currentTimeMillis();
		this.exception = exception;
		this.data = data;
	}

	/*************************************************************************/
	public static <T> Result<T> successNoData() {
		return new Result<>(ResultCode.SUCCESS);
	}

	public static <T> Result<T> successNoData(String message) {
		return new Result<>(ResultCode.SUCCESS, message);
	}

	public static <T> Result<T> successData(T data) {
		return new Result<T>(ResultCode.SUCCESS, data);
	}

	public static <T> Result<T> fail(IResultCode resultCode) {
		return new Result<>(resultCode);
	}

	public static <T> Result<T> fail(IResultCode resultCode, String exception) {
		return new Result<>(resultCode, exception);
	}
	
	public static <T> Result<T> fail(String message) {
		return new Result<>(ResultCode.SERVER_EXCEPTION.getStatus(), message, "", null);
	}
}
